# -*- coding: utf-8 -*-

import scrapy
import time
import math
import json
from snowball_crawler.items import SnowballStockbarItem
from snowball_crawler.spiders import snowball_spider

class snowball_stock_bar_spider( snowball_spider.snowball_base_spider):
    '''
    证券日数据spider
    '''
    name ="snowball_stock_bar"
    allowed_domains=["xueqiu.com"]
    format_url = 'https://xueqiu.com/stock/forchartk/stocklist.json?symbol=%s&period=1day&type=%s&begin=%d000&end=%d000'

    time_offset = 1E8  #大约等于3.16year
    curr_time = math.trunc(time.mktime(time.localtime()))

    #实现start_spider方法 登录成功后会调用
    def start_spider(self):
        stock_list= ['SH600000','AAPL','SZ000001','CHA','SZ159919','SH600570']
        price_type =['before' ,'normal', 'after'] #前复权 、不复权、后复权，其中前复权数据在分红派息之后需要更新

        for type in price_type:
            for symbol in stock_list:
                for i in range(10) : #相当于18年多 大约是1999年到今天的数据
                   meta = {'begin': self.curr_time - (i+1)*self.time_offset, 'end': self.curr_time- i*self.time_offset, 'symbol':symbol, 'bar_type':type}
                   yield scrapy.Request(url=self.format_url%(meta['symbol'],meta['bar_type'], meta['begin'],meta['end']),
                                     headers=self.headers,
                                     meta=meta,
                                     callback=self.parse)

    #解析bar数据 如果为空终止 否则回调
    def parse(self, response):
        html = response.body.decode()
        dic = json.loads(html)
        if dic:
            if len(dic['chartlist']) > 0:
                item = SnowballStockbarItem()
                item['symbol'] = response.meta['symbol']
                item['bartype'] =response.meta['bar_type']
                item['bardata'] = dic['chartlist']
                return item
